/**
 * @Description : 方法工具类
 * @author : August_xiao 1932794922@qq.com
 * @date : 2022-03-05 23:32
 */
import { customRef } from 'vue'
import store from '@/store'

/**
 * 判读设备信息
 * @returns {RegExpMatchArray}
 * @private
 */
const _isMobile = () => {
  return navigator.userAgent.match(
    /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
  )
}

/**
 * 自定义防抖
 * @param value
 * @param delay
 * @returns {Ref<*>}
 */
const useDebouncedRef = (value, delay = 200) => {
  let timeout = null
  return customRef((track, trigger) => {
    return {
      get() {
        track()
        return value
      },
      set(newValue) {
        clearTimeout(timeout)
        timeout = setTimeout(() => {
          value = newValue
          trigger()
        }, delay)
      }
    }
  })
}

/**
 * 自定义节流
 * @param value
 * @param delay
 * @returns {Ref<*>}
 */
const useThrottleRef = (value, delay = 200) => {
  let canRun = true
  return customRef((track, trigger) => {
    return {
      get() {
        track()
        return value
      },
      set(newVal) {
        if (!canRun) return
        canRun = false
        setTimeout(() => {
          value = newVal
          canRun = true
          trigger()
        }, delay)
      }
    }
  })
}

/**
 * 防抖
 * @param fn
 * @param delay
 * @returns {(function(): void)|*}
 */
const debounce = (fn, delay = 200) => {
  let timer = null
  return function () {
    let args = arguments
    if (timer) {
      clearTimeout(timer)
    }
    timer = setTimeout(() => {
      fn.apply(this, args) // this 指向vue
    }, delay)
  }
}

/**
 * 节流
 * @param fn
 * @param delay
 * @returns {(function(): void)|*}
 */
const throttle = (fn, delay) => {
  let timer = null
  return function () {
    let context = this,
      args = arguments
    clearTimeout(timer)
    timer = setTimeout(function () {
      fn.apply(context, args)
    }, delay)
  }
}

/**
 * 滚动锚点
 * @param where id
 */
const headerTopDown = (where) => {
  $('html,body').animate(
    {
      scrollTop: $(where).offset().top
    },
    500
  )
}

/**
 * 设备icon图标显示
 * @param str
 * @returns {string}
 */
const iconDeviceChoice = (str) => {
  if (str) {
    if (new RegExp('Mac|iPhone', 'gi').test(str)) return 'fa-apple'
    if (new RegExp('Windows', 'gi').test(str)) return 'fa-windows'
    if (new RegExp('Android', 'gi').test(str)) return 'fa-android'
  }
}

/**
 * 浏览器icon图标
 * @param str
 * @returns {string}
 */
const iconBrowserChoice = (str) => {
  if (str) {
    if (new RegExp('Chrome', 'gi').test(str)) return 'fa-chrome'
    if (new RegExp('edge', 'gi').test(str)) return 'fa-edge'
    if (new RegExp('Safer|safari', 'gi').test(str)) return ' fa-safari'
    if (new RegExp('google', 'gi').test(str)) return 'fa-google'
    if (new RegExp('ie', 'gi').test(str)) return 'fa-internet-explorer'
    if (new RegExp('Open', 'gi').test(str)) return 'fa-opera'
    if (new RegExp('Firefox', 'gi').test(str)) return 'fa-firefox'
  }
}

/**
 * 主题选择
 * @param event
 */
const choiceTheme = (event) => {
  // 获取当前点击元素id
  if (event.currentTarget.id !== 'dark-bg') {
    $('html').attr('data-theme', 'light')
    store.commit('RECORD_SET_THEME', true)
  }

  switch (event.currentTarget.id) {
    case 'white-bg':
      $('body').css('background-image', 'none')
      $('html').css('background', 'none')
      window.localStorage.setItem('theme', 'true')
      break
    case 'sakura-bg':
      $('body').css(
        'background-image',
        'url(https://view.moezx.cc/images/2018/01/03/sakura.png'
      )
      break
    case 'gribs-bg':
      $('body').css(
        'background-image',
        'url(https://view.moezx.cc/images/2018/01/03/plaid2dbf8.jpg'
      )
      break
    case 'KAdots-bg':
      $('body').css(
        'background-image',
        'url(https://view.moezx.cc/images/2018/01/24/kyotoanimation.png'
      )
      break
    case 'totem-bg':
      $('body').css(
        'background-image',
        'url(https://view.moezx.cc/images/2018/09/17/little-monster.png'
      )
      break
    case 'pixiv-bg':
      $('body').css(
        'background-image',
        'url(https://view.moezx.cc/images/2018/01/10/star02.png'
      )
      break
    case 'bing-bg':
      // $('body').css('background-image', `url(https://apikey.net/?type=Himg&time=${new Date().getTime()}`)
      $('body').css(
        'background-image',
        'url(https://zfe.space/images/tianqizhizi.jpg'
      )
      break
    case 'dark-bg':
      $('html').attr('data-theme', 'dark')
      $('body').css('background-image', 'none')
      store.commit('RECORD_SET_THEME', false)
      window.localStorage.setItem('theme', 'false')
      break
  }
}

export {
  _isMobile,
  useDebouncedRef,
  useThrottleRef,
  debounce,
  throttle,
  headerTopDown,
  iconBrowserChoice,
  iconDeviceChoice,
  choiceTheme
}
